home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry1.iso / mAz Lite 1.0 / camera.wdl < prev    next >
Encoding:
Text File  |  2003-03-17  |  13.5 KB  |  519 lines

  1. // Template file v5.202 (02/20/02)
  2. ////////////////////////////////////////////////////////////////////////
  3. // File: camera.wdl
  4. //        WDL prefabs for camera movment
  5. ////////////////////////////////////////////////////////////////////////
  6. // Use:
  7. //        Include AFTER "movment.wdl"
  8. //
  9.  
  10. IFNDEF CAMERA_ORBIT_DEFS;
  11.     DEFINE kOrbitCameraPanInc, 5;
  12.     DEFINE kOrbitCameraDistInc, 5;
  13.     DEFINE kOrbitCameraZOffInc, 5;
  14. ENDIF;
  15.  
  16. //@ Camera Vars
  17. //#var person_3rd    = 0;        // 0: 1st person mode; 0.5-1: 3rd person mode  DEFINED in movement.wdl
  18.  
  19. var camera_solidpass = 0;            // 0: avoid passing into solids (may cause
  20.                                             //   shaky camera)
  21.                                             // 1: pass through solids
  22.  
  23. var camera_dist[3] = 90,0,0;               // camera distance to entity in 3rd person view
  24.  
  25. var chase_camera_dist[3] = -20,90,0;  // camera distance to entity in chase view
  26.  
  27.  
  28. var orbit_camera_pan = 180;   // pan around center point
  29. var orbit_camera_dist = 150;  // distance from center point
  30. var orbit_camera_zOff = 5;        // distance up
  31.  
  32. ///////////////////////////////////////////////////////////////////////
  33. var camera_speed[3] = 0,0,0;    // cartesian speed, entity coordinates
  34. var camera_aspeed[3];        // angular speed
  35.  
  36. var    current_fog_index    = 1;    // the current fog color index
  37.  
  38. var walk_rate = 3;     // for head wave, 360 / step width
  39. var wave_rate = 25;     // same for swimming, 360 / wave time
  40. var walk_ampl = 4;    // walking head wave amplitude
  41. var wave_ampl = 2;     // swimming head wave amplitude
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. //@ Function prototypes
  49. //ACTION camera_move    - Attach to entity in a level without player, free move camera
  50. function move_view_1st();    // Handles first person camera view
  51. function move_view_3rd();  // Handles third person camera view
  52. function move_view_orbit();// Handles the 'orbit' camera
  53. function move_view_chase();// Handles the 'chase' camera
  54. function move_view();        // Call the appropriate function to move the camera
  55. function toggle_person();    // Toggle between 1st and 3rd person views
  56. function cycle_person_view();// Cycle from 1st to 3rd to orbit person views
  57. function set_pos_ahead();  // Calculate a position directly ahead of the camera
  58. function _set_pos_ahead_xyz();    // Calculate a position ahead of the player
  59.  
  60.  
  61. //@ Function Code
  62.  
  63. /////////////////////////////////////////////////////////////////////////
  64. // Desc: This is the main movement action for the camera
  65. //
  66. ACTION camera_move
  67. {
  68.     _camera = 1;
  69.     while(_camera == 1)
  70.     {
  71.         _player_intentions();     // set force and aforce value from user input
  72.  
  73.          // -old method- ACCEL    aspeed,aforce,0.9;
  74.         camera_aspeed.pan  += (TIME * aforce.pan)  - (0.9 * camera_aspeed.pan);
  75.         camera_aspeed.tilt += (TIME * aforce.tilt) - (0.9 * camera_aspeed.tilt);
  76.         camera_aspeed.roll += (TIME * aforce.roll) - (0.9 * camera_aspeed.roll);
  77.  
  78.         CAMERA.PAN += camera_aspeed.PAN * TIME;
  79.         CAMERA.TILT += camera_aspeed.TILT * TIME;
  80.         CAMERA.ROLL += camera_aspeed.ROLL * TIME;
  81.  
  82.         // Calculate camera's new speed
  83.          // -old method- ACCEL    speed,force,0.9;
  84.         camera_speed.x += (TIME * force.x) - (0.9 * camera_speed.x);
  85.         camera_speed.y += (TIME * force.y) - (0.9 * camera_speed.y);
  86.         camera_speed.z += (TIME * force.z) - (0.9 * camera_speed.z);
  87.  
  88.         // calculate relative distance
  89.        dist.x = camera_speed.x * TIME;
  90.           dist.y = camera_speed.y * TIME;
  91.           dist.z = camera_speed.z * TIME;
  92.  
  93.         // Replace move_view with XYZ displacement
  94.         //move_view CAMERA,dist,NULLSKILL;
  95.         vec_rotate(dist.x,CAMERA.pan);
  96.         vec_add(CAMERA.X, dist.X);
  97.  
  98.         wait(1);
  99.     }
  100. }
  101.  
  102.  
  103. ///////////////////////////////////////////////////////////////////////
  104. // First person camera view
  105. // This should be a client-only action!!
  106. //
  107. function move_view_1st()
  108. {
  109.     if(_camera == 0) // If the camera does not move itself
  110.     {
  111.         // Position the camera
  112.  
  113.         CAMERA.DIAMETER = 0;        // make the camera passable
  114.         CAMERA.GENIUS = player;    // don't display parts of ME
  115.           CAMERA.X = player.X;    // place camera at player's location
  116.         CAMERA.Y = player.Y;
  117.         CAMERA.Z = player.Z + player.MIN_Z;  // start at 'feet', move up later...
  118.  
  119.         // Move the eye height up depending on the _MOVEMODE (start at feet)
  120.          if(player._MOVEMODE == _MODE_SWIMMING)
  121.         {
  122.             // adjust eye height for swimming
  123.               CAMERA.Z += (player.MAX_Z-player.MIN_Z)*eye_height_swim;
  124.         }
  125.         else  // not swimming
  126.         {
  127.  
  128.              if((player._MOVEMODE == _MODE_DUCKING) || (player._MOVEMODE == _MODE_CRAWLING))
  129.             {
  130.                 // adjust eye height for ducking and crawling
  131.                 CAMERA.Z += (player.MAX_Z-player.MIN_Z)*eye_height_duck;
  132.             }
  133.             else
  134.             {
  135.                 // adjust eye height for 'normal' modes
  136.                 CAMERA.Z += (player.MAX_Z-player.MIN_Z)*eye_height_up;
  137.             }
  138.          }
  139.  
  140.         CAMERA.PAN = player.PAN;
  141.         CAMERA.TILT = player.TILT + head_angle.TILT;
  142.         CAMERA.ROLL = player.ROLL;
  143.  
  144.         // Handle head-bob
  145.  
  146.         if(my_height < 5 || (player._MOVEMODE == _MODE_SWIMMING) )
  147.         {
  148.             // use
  149.             headwave = sin(player_dist*walk_rate);
  150.  
  151.             if((player._MOVEMODE == 0)    // moving on client?
  152.                 || (player._MOVEMODE == _MODE_WALKING))
  153.             {
  154.                 // Play the right and left foot sound
  155.                 if(((headwave > 0) && (walkwave <= 0))
  156.                     || ((headwave <= 0) && (walkwave > 0)))
  157.                 {
  158.                     play_sound(thud,30);
  159.                 }
  160.                 // head bobbing
  161.                 walkwave = headwave;
  162.                 headwave = walk_ampl*(abs(headwave)-0.5);
  163.             }
  164.  
  165.             if((player._MOVEMODE == _MODE_SWIMMING) && (ent_content(NULL,CAMERA.x) != CONTENT_PASSABLE))//(on_passable_ == ON))
  166.             {
  167.                 if((headwave > 0) && (walkwave <= 0))
  168.                 {
  169.                     play_sound(splash,30);
  170.                 }
  171.                 // in-water wave movement
  172.                 walkwave = headwave;
  173.                 headwave = wave_ampl*sin(TOTAL_TICKS*wave_rate);
  174.                 head_angle.TILT += 0.1*wave_ampl*sin(TOTAL_TICKS*wave_rate - 60);
  175.             }
  176.         } // END if(my_height < 5 || (player._MOVEMODE == _MODE_SWIMMING) )
  177.  
  178.  
  179.         if(player.__BOB == ON) { CAMERA.Z += headwave;    }
  180.  
  181.  
  182.  
  183. // check to see if camera is located in a passable block and set fog color index
  184. //jcl 07-22-00  old fog is saved
  185.         if (ent_content(NULL,CAMERA.x) == CONTENT_PASSABLE)
  186.         {
  187.             if (FOG_COLOR != _FOG_UNDERWATER)
  188.             {
  189.                 current_fog_index = FOG_COLOR;    // save old fog
  190.                 FOG_COLOR = _FOG_UNDERWATER;    // set fog color to underwater fog
  191.             }
  192.         }
  193.         else
  194.         {
  195.             if (FOG_COLOR == _FOG_UNDERWATER)
  196.             {
  197.                 // else restore current_fog_index
  198.                 FOG_COLOR = current_fog_index;
  199.             }
  200.         }
  201.  
  202.         person_3rd = 0;  // we are in first person mode
  203.  
  204.     } // END if(_camera == 0) // If the camera does not move itself
  205. }
  206.  
  207. /////////////////////////////////////////////////////////////////////////
  208. // Desc: Third person camera view
  209. //
  210. function move_view_3rd()
  211. {
  212.     if ((_camera == 0) && (player != NULL))
  213.     {
  214.  
  215.          CAMERA.DIAMETER = 0;        // make the camera passable
  216.         CAMERA.genius = player;
  217.         CAMERA.pan += 0.2 * ang(player.pan-CAMERA.pan);
  218.  
  219.         // tilt the camera differently if we are using a vehicle
  220.          if ( (player._MOVEMODE == _MODE_PLANE)
  221.              ||(player._MOVEMODE == _MODE_CHOPPER))
  222.          {
  223.              CAMERA.tilt += 0.2 * ang(player.tilt-CAMERA.tilt);
  224.          }
  225.         else
  226.         {
  227.             // walking, swimming etc.
  228.             CAMERA.TILT = head_angle.TILT;
  229.  
  230.                 if((person_3rd < 1) && (camera_dist.Z == 0))    // switching to 3rd person
  231.             {
  232.                 camera_dist.Z = -(player.MAX_Z-player.MIN_Z)*eye_height_up;//- player.MAX_Z;
  233.             }
  234.  
  235.  
  236.         }
  237.  
  238.         vec_set(temp,temp_cdist);      // temp = temp_cdist
  239.         // don't tilt camera if swimming
  240.         if(player._MOVEMODE == _MODE_SWIMMING)
  241.         {
  242.             temp2 = player.TILT;
  243.             player.TILT = 0;
  244.             vec_rotate(temp,player.PAN);
  245.             player.TILT = temp2;
  246.         }
  247.         else
  248.         {
  249.             vec_rotate(temp,player.PAN);
  250.         }
  251.       CAMERA.X += 0.3*(player.X - temp.X - CAMERA.X);
  252.       CAMERA.Y += 0.3*(player.Y - temp.Y - CAMERA.Y);
  253.       CAMERA.Z += 0.3*(player.Z - temp.Z - CAMERA.Z);
  254.  
  255.          // test if camera is IN_PASSABLE or IN_SOLID
  256.         temp = ent_content(NULL,CAMERA.X);
  257.  
  258.         // if camera moved into a wall...
  259.         if((temp == CONTENT_SOLID) && (camera_solidpass == 0))
  260.         {
  261.             temp_cdist.X *= 0.7;    // place it closer to the player
  262.             temp_cdist.Y *= 0.7;
  263.             temp_cdist.Z *= 0.7;
  264.         }
  265.         else
  266.         {
  267.             temp_cdist.X += 0.2*(player.MAX_X + camera_dist.X - temp_cdist.X);
  268.             temp_cdist.Y += 0.2*(player.MAX_Y + camera_dist.Y - temp_cdist.Y);
  269.             temp_cdist.Z += 0.2*(player.MAX_Z + camera_dist.Z - temp_cdist.Z);
  270.         }
  271.  
  272.         // check to see if camera is located in a passable block and set fog color index
  273.         if (temp == CONTENT_PASSABLE)
  274.         {
  275.             if (FOG_COLOR != _FOG_UNDERWATER)
  276.             {
  277.                 current_fog_index = FOG_COLOR;    // save old fog
  278.                 FOG_COLOR = _FOG_UNDERWATER;         // set fog color to underwater fog
  279.             }
  280.         }
  281.         else
  282.         {
  283.             if (FOG_COLOR == _FOG_UNDERWATER)
  284.             {
  285.                 // else restore current_fog_index
  286.                 FOG_COLOR = current_fog_index;
  287.             }
  288.         }
  289.         person_3rd = 1;     // fully 3rd person
  290.     }
  291.  
  292. }
  293.  
  294. /////////////////////////////////////////////////////////////////////////
  295. // Desc: functions used to change the orbit camera values
  296. function inc_orbit_camera_pan()
  297. {
  298.     orbit_camera_pan += kOrbitCameraPanInc;
  299.     IF (orbit_camera_pan > 360)
  300.     {
  301.         orbit_camera_pan -= 360;
  302.     }
  303. }
  304. function dec_orbit_camera_pan()
  305. {
  306.     orbit_camera_pan -= kOrbitCameraPanInc;
  307.     IF (orbit_camera_pan < 0)
  308.     {
  309.         orbit_camera_pan += 360;
  310.     }
  311. }
  312. function inc_orbit_camera_dist()
  313. {
  314.     orbit_camera_dist += kOrbitCameraDistInc;
  315. }
  316. function dec_orbit_camera_dist()
  317. {
  318.     orbit_camera_dist -= kOrbitCameraZOffInc;
  319.     IF (orbit_camera_pan < 0)
  320.     {
  321.         orbit_camera_pan = 0;
  322.     }
  323. }
  324. function inc_orbit_camera_zOff()
  325. {
  326.     orbit_camera_zOff += kOrbitCameraDistInc;
  327. }
  328. function dec_orbit_camera_zOff()
  329. {
  330.     orbit_camera_zOff -= kOrbitCameraZOffInc;
  331. }
  332.  
  333. /////////////////////////////////////////////////////////////////////////
  334. // Desc: Orbit camera
  335. //
  336. function move_view_orbit()
  337. {
  338.      CAMERA.DIAMETER = 0;        // make the camera passable
  339.      CAMERA.GENIUS = PLAYER;
  340.      CAMERA.X = PLAYER.X + (orbit_camera_dist * SIN(orbit_camera_pan));
  341.      CAMERA.Y = PLAYER.Y + (orbit_camera_dist * COS(orbit_camera_pan));
  342.      CAMERA.Z = PLAYER.Z + orbit_camera_zOff;
  343.  
  344.      // if the camera is IN_PASSABLE (set by TOUCH) assume it is underwater
  345.     TOUCH    NULL,CAMERA.POS;
  346.      IF (IN_PASSABLE)
  347.      {
  348.           FOG_COLOR = _FOG_UNDERWATER;  // set fog color to underwater
  349.      }
  350.      ELSE
  351.      {
  352.          // else restore the current_fog_index
  353.          FOG_COLOR = current_fog_index;
  354.      }
  355.  
  356.      // face the player
  357.      temp.X = player.X - camera.X;
  358.    temp.Y = player.Y - camera.Y;
  359.    temp.Z = player.Z - camera.Z;
  360.      TO_ANGLE temp,temp;
  361.      camera.PAN = temp.PAN;
  362.      camera.TILT = temp.TILT;
  363. }
  364.  
  365.  
  366.  
  367. /////////////////////////////////////////////////////////////////////////
  368. // Desc: Chase camera
  369. //
  370.  
  371. var    chase_camera_ang[3];
  372.  
  373. function move_view_chase()
  374. {
  375.     if ((_camera == 0) && (player != NULL))
  376.     {
  377.  
  378.          CAMERA.DIAMETER = 0;        // make the camera passable
  379.         CAMERA.genius = player;
  380.  
  381.  
  382.  
  383.           // calculate the camera view direction angles to the player
  384.        vec_diff(temp,nullvector,chase_camera_dist);    // temp = -camera_dist
  385.        vec_to_angle(chase_camera_ang,temp);      // chase_camera_ang points towards player
  386.        chase_camera_ang.roll = 0;                     // zero out roll angle
  387.  
  388.         // place the camera at the right position to the ship
  389.       vec_set(camera.x,chase_camera_dist);
  390.       vec_rotate(camera.x,player.pan);
  391.       vec_add(camera.x,player.x);
  392.         // set the camera angles to the player's angles
  393.       vec_set(camera.pan,player.pan);
  394.         // and quaternion rotate them by the camera view direction angles
  395.       ang_rotate(camera.pan,chase_camera_ang);
  396.     }
  397. }
  398.  
  399. /////////////////////////////////////////////////////////////////////
  400. // Desc: Call the appropriate function to move the camera
  401. //
  402. //    Uses person_3rd value for branching
  403. function move_view()
  404. {
  405.     if(player == NULL) { player = ME; }    // this action needs the player pointer
  406.     if(player == NULL) { return; }            // still no player -> can't work
  407.  
  408.     if(person_3rd > 2)
  409.     {
  410.         move_view_chase();
  411.         return;
  412.     }
  413.  
  414.     if(person_3rd > 1)
  415.     {
  416.         move_view_orbit();
  417.         return;
  418.     }
  419.  
  420.     if(person_3rd > 0)
  421.       {
  422.         move_view_3rd();
  423.         return;
  424.     }
  425.  
  426.     // default 1st person view
  427.     move_view_1st();
  428.  
  429. }
  430.  
  431.  
  432. /////////////////////////////////////////////////////////////////////
  433. // Desc: Toggle between 1st and 3rd person views
  434. //
  435. //    Effects 'person_3rd' value
  436. function toggle_person()
  437. {
  438.     if(person_3rd > 0)
  439.     {
  440.         person_3rd = 0;
  441.     }
  442.     else
  443.     {
  444.         person_3rd = 0.5;
  445.     }
  446. }
  447.  
  448. /////////////////////////////////////////////////////////////////////
  449. // Desc: Cycle from 1st to 3rd to orbit person views
  450. //
  451. //    Effects 'person_3rd' value
  452. function cycle_person_view()
  453. {
  454.     if(person_3rd > 2)   // in 'chase' range
  455.     {
  456.         person_3rd = 0; // switch to 1st person view
  457.         return;
  458.     }
  459.  
  460.     if(person_3rd > 1)   // in 'orbit' range
  461.     {
  462.         person_3rd = 3; // switch to chase person view
  463.         return;
  464.     }
  465.  
  466.     if(person_3rd > 0)  // 3rd person veiw
  467.     {
  468.         person_3rd = 2;  // switch to orbit
  469.        return;
  470.     }
  471.  
  472.      person_3rd = 0.5; // switch to 3rd person view
  473.  
  474.  }
  475.  
  476.  
  477. /////////////////////////////////////////////////////////////
  478. // Desc: Calculate a position directly ahead of the camera
  479. // Input:  p (distance)
  480. // Output: MY_POS
  481. function set_pos_ahead()
  482. {
  483.     temp.X = cos(CAMERA.PAN);
  484.     temp.Y = sin(CAMERA.PAN);
  485.     temp.Z = p*cos(CAMERA.TILT);
  486.     MY_POS.X = CAMERA.X + temp.Z*temp.X;
  487.     MY_POS.Y = CAMERA.Y + temp.Z*temp.Y;
  488.     MY_POS.Z = CAMERA.Z + p*sin(CAMERA.TILT);
  489. }
  490.  
  491. /////////////////////////////////////////////////////////////
  492. // Desc: Calculate a 3d position relative to the camera angles
  493. // Input:  MY_POS
  494. // Output: MY_POS
  495. //
  496. function _set_pos_ahead_xyz()
  497. {
  498.     vec_rotate(MY_POS,CAMERA.PAN);
  499.     if(person_3rd != 0)
  500.     {
  501.         MY_POS.X += player.X;
  502.         MY_POS.Y += player.Y;
  503.         MY_POS.Z += player.Z;
  504.     }
  505.     else
  506.     {
  507.         MY_POS.X += CAMERA.X;
  508.         MY_POS.Y += CAMERA.Y;
  509.         MY_POS.Z += CAMERA.Z;
  510.     }
  511. }
  512.  
  513.  
  514.  
  515.  
  516.  
  517. // Define ON_KEY functions
  518. //ON_F7 toggle_person;
  519. ON_F7    cycle_person_view;